# display patch help
help_patch(){
	echo ""
	echo "=========================================="
	echo "Environment options [dev, uat, pro]"
	echo "Patching Guide: at 'pro' environment"
	echo "=========================================="
	echo ""
	echo "Step 1: Upload latest code to directory 'sandbox'."
	echo "	- Change setting in 'sandbox/Environment.php'."
	echo "	- Ensure you have latest DB schema updated."
	echo ""
	echo "Step 2: Patch 'sandbox' command. Eg: -e dev|uat|pro"
	echo "	sh $0 -e pro -m sandbox"
	echo ""
	echo "Step 3: Patch 'main' command. Eg: -e dev|uat|pro"
	echo "	sh $0 -e pro"
	echo ""
	echo "Step 4: Revert patch command (in case latest code not work). Eg: -e dev|uat|pro"
	echo "	sh $0 -e pro -r"
	echo ""
	echo ""
	echo "========================================="
	echo "Detail Usage: $0 -e -m -r"
	echo "========================================="
	echo "Options: [compulsary]"
	echo "	-e Environment. Arguments [dev or uat or pro]."
	echo ""
	echo "Options: [optional]"
	echo "	-m Mode. Arguments [main or sandbox]. default: main."
	echo "	-r Revert."
	echo ""
	exit 1
}
REVERT=false;
MODE="main";
while getopts ":e:m:r" option;
do
	case $option in
		e) ENV=$OPTARG;;
		m) MODE=$OPTARG;;
		r) REVERT=true;;
		:) echo "option -$OPTARG needs an argument. Type -h for help."; exit;;
		\?) help_patch;;
	esac
done

if [ -z $ENV ]
then
	help_patch

elif [ $ENV = 'dev' -o $ENV = 'uat' -o $ENV = 'pro' ]
then
	echo ""
	echo "==================================="
	
	if $REVERT
	then
		echo "Reverting '$MODE'";
	else
		echo "Start Patching '$MODE'";
	fi
	echo "==================================="
else
	echo "Invalid argument for option -e: [dev or uat or pro]?";
	exit;
fi

case $ENV in
	'dev') HOSTNAME='devweddi';;
	'uat') HOSTNAME='uatweddi';;
	'pro') HOSTNAME='weddingc';;
esac


if [ $MODE = 'sandbox' ]
then
	USELESS_DIR=(
		sandbox/.git
		sandbox/nbproject
		sandbox/phpdoc
		sandbox/tools
		sandbox/README.md
	);
	for dir in "${USELESS_DIR[@]}"
	do
		rm -f -R "${dir}";
		echo "'${dir}': removed.";
	done

	mkdir -p -v sandbox/assets

	CLEAR_DIR=(
		sandbox/assets/*
		sandbox/protected/runtime/*
	);
	for dir in "${CLEAR_DIR[@]}"
	do
		rm -f -R "${dir}";
		echo "'${dir}': cleared.";
	done

	WRITABLE_DIR=(
		sandbox/assets
		sandbox/themes
		sandbox/protected/runtime
		sandbox/protected/data
	);
	for dir in "${WRITABLE_DIR[@]}"
	do
		chmod -R 777 "${dir}";
		echo "'${dir}': changed to 0777 (rwxrwxrwx)";
	done

	ln -v -s "/home/${HOSTNAME}/public_html/uploads" "/home/${HOSTNAME}/public_html/sandbox/uploads"
	cp -v -f "sandbox/setup/env_config/.htaccess_${ENV}" "sandbox/setup/.htaccess";
	cp -v -f "sandbox/env_config/.htaccess_${ENV}_${MODE}" "sandbox/.htaccess";
	nano sandbox/Environment.php
	echo "";
	echo "Done";
	echo ""
	exit;
fi

CURRENTTIME=$(date +"%b_%d_%Y")

if $REVERT
then
	rmdir -v sandbox;
	mv -v main sandbox;
	cp -v -f "sandbox/env_config/.htaccess_${ENV}_sandbox" "sandbox/.htaccess";
	mv -v "main_${CURRENTTIME}" main;
else
	if [ -d "main_${CURRENTTIME}" ]
	then
		SUFFIX=$(date +"%T")
		mv -v "main_${CURRENTTIME}" "main_${CURRENTTIME}_${SUFFIX}";
	fi
	mv -v main "main_${CURRENTTIME}";
	mv -v sandbox main;
	
	rm -v main/uploads;
	ln -v -s "/home/${HOSTNAME}/public_html/uploads" "/home/${HOSTNAME}/public_html/main/uploads";
	cp -v -f "main/env_config/.htaccess_${ENV}_main" "main/.htaccess";
	cp -v -f "main/env_config/robots_${ENV}.txt" "main/robots.txt";
	rm -f -R main/setup;
	mkdir -v sandbox;
fi

echo "";
echo "Done";
echo "";
